package com.example.passport.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.common.util.StrUtil;
import com.example.interceptor.UserContext;
import com.example.passport.dto.UserCreateDto;
import com.example.passport.dto.UserDto;
import com.example.passport.entity.User;
import com.example.passport.entity.UserRole;
import com.example.passport.service.UserRoleService;
import com.example.passport.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "用户管理")
@RestController
@CrossOrigin
@RequestMapping("/passport/user")
public class UserController {
    @Resource
    public UserService userService;
    @Resource
    UserRoleService userRoleService;

    @ApiOperation("获取用户信息")
    @GetMapping("/whoami")
    public JsonResult<UserDto> whoami() {

        User user = UserContext.getUser();

        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);

        return new JsonResult<>(userDto);
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public JsonResult<ListDto<UserDto>> list(User condition, Pagination pagination) {
        User user = UserContext.getUser();
        condition.setTenantId(user.getTenantId());
        return new JsonResult<>(userService.listUser(condition, pagination));
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid UserDto userDto) {
        userService.update(userDto);
        return new JsonResult<>();
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public JsonResult<String> updatePassword(String oldPwd, String newPwd) {
        User user = UserContext.getUser();
        if(newPwd=="" || newPwd==null){
            throw new BusinessException("密码不能为空");
        }
        userService.updatePassword(user,oldPwd,newPwd);

        return new JsonResult<>();
    }

    @ApiOperation("修改状态")
    @PostMapping("/updateStatus")
    public JsonResult<UserDto> updateStatus(long userId, int status) {
        User user = UserContext.getUser();

        userService.updateStatus(userId,status,user);

        return new JsonResult<>();
    }

    @ApiOperation("添加用户（员工）")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid UserCreateDto userCreateDto) {
        User me = UserContext.getUser();
        User user = new User();
        user.setTenantId(me.getTenantId());
        user.setUsername(userCreateDto.getUsername());
        user.setMobile(userCreateDto.getMobile());
        user.setName(userCreateDto.getName());
        user.setApiToken(StrUtil.randomString(6));
        String list= StringUtils.join(userCreateDto.getEventId(),",");
        user.setEventId(list);
        user.setSuperiorId(userCreateDto.getSuperiorId());
        user.setToUser(userCreateDto.getToUser());
        userService.createUser(user, userCreateDto.getPassword());

        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        userRole.setTenantId(me.getTenantId());
        userRole.setRoleId(2L); //默认新增维修班组角色
        userRoleService.createUserRole(userRole);

        return new JsonResult<>();
    }

    @ApiOperation("删除数据")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
        userService.delete(id);
        return new JsonResult<>();
    }

}
